package cn.com.huak.www.sell.controller;

import cn.com.huak.www.sell.dto.OrderDTO;
import cn.com.huak.www.sell.enums.ResultEnum;
import cn.com.huak.www.sell.exception.OrderException;
import cn.com.huak.www.sell.service.OrderMasterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author huak@wanggang
 * @date 2019/1/20 15:50
 */
@RequestMapping("/seller/order")
@Controller
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderMasterService orderMasterService;

    /**
     * 订单列表
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="list", method = RequestMethod.GET)
    public String list(@RequestParam(value="page", defaultValue = "1") Integer page,
                       @RequestParam(value="size", defaultValue = "10") Integer size,
                       Model model){
        //1. 查询订单列表
        PageRequest pageRequest = new PageRequest(page - 1, size);
        Page<OrderDTO> list = orderMasterService.findList(pageRequest);

        //2. 将订单列表放在model中
        model.addAttribute("page", list);
        model.addAttribute("currentPage", page);
        model.addAttribute("size", size);

        //3. 返回结果
        return "order/list";
    }

    /**
     * 取消订单
     * @param orderId 订单id
     * @return
     */
    @RequestMapping(value="cancel", method = RequestMethod.GET)
    public String cancel(@RequestParam("orderId") String orderId,
                          Model model){
        try{
            //1. 查询订单
            OrderDTO one = orderMasterService.findOne(orderId);

            //2. 取消订单
            orderMasterService.cancel(one);
            //3. 返回提示信息
            model.addAttribute("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
            model.addAttribute("url", "/sell/seller/order/list");
            return "common/success";

        }catch (OrderException e){
            log.error("【卖家端取消订单】发生错误:{}", e.getMessage(), e);
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url", "/sell/seller/order/list");
            return "common/error";
        }
    }

    /**
     * 订单详情
     * @param orderId
     * @param model
     * @return
     */
    @RequestMapping("detail")
    public String detail(@RequestParam("orderId") String orderId,
                         Model model){
        try{
            //1. 查询订单
            OrderDTO one = orderMasterService.findOne(orderId);
            model.addAttribute("orderDTO", one);
            return "order/detail";
        }catch (OrderException e){
            log.error("【卖家端查询订单】查询订单详情发生错误...", e);
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url", "/sell/seller/order/list");
            return "common/error";
        }
    }

    /**
     * 完结订单
     * @param orderId 订单id
     * @param model
     * @return
     */
    @RequestMapping(value="finish", method = RequestMethod.GET)
    public String finish(@RequestParam("orderId") String orderId,
                         Model model){
        try{
            //1. 查询订单
            OrderDTO one = orderMasterService.findOne(orderId);

            //2. 取消订单
            orderMasterService.finish(one);
            //3. 返回提示信息
            model.addAttribute("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
            model.addAttribute("url", "/sell/seller/order/list");
            return "common/success";

        }catch (OrderException e){
            log.error("【卖家端完结订单】发生错误:{}", e.getMessage(), e);
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url", "/sell/seller/order/list");
            return "common/error";
        }
    }
}
